<?php 
/**
 * 销售单欠款 object_type 120 
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	账目信息
 * @package  	Model
 * @author    	李恺弘
 * @version 	2.1,2012-07-22
 */
class ClientSalePublicModel extends AbsFundsPublicModel {
	/// 模型名与数据表名不一致，需要指定
	public $tableName 				= 'paid_detail';
	///款项类型
	public $object_type				=	120;///客户期初欠款
	///对象类型
	public $comp_type				=	1;
	///对象款项表
	public $object_type_table		=	'client_paid_detail';	
   
 	/**
	 * 销售产生的款项
	 *
	 * @param array $info $info['id'] 销售单号 $info['info'] 页面post来的原始信息
	 * @return array $vo
	 */
	public function _fund($info){   
///	 	$post		=&	$info['post']; 
		$sale_id	=	$info['id'];///销售单ID 
		///销售单欠款 
		$sale_funds		=	$this->saleFunds($sale_id); 	
		if(is_array($sale_funds) && $sale_funds['id']>0) {
			$paid_for[]	=	$sale_funds['id'];
		} 
 		$vo['sale_id']	=	$sale_id; 
		$vo['advance']	=	array('cash'=>$info['cash'],'bill'=>$info['bill'],'transfer'=>$info['transfer'],'post'=>$info);  
		///预付款  
///		if (C('SHOW_SALE_ADVANCE')==1){
			/// <!--{*开启销售单预收款*}-->
			$saleAdvance		=	$this->saleAdvance($vo); 
			if(is_array($saleAdvance) ) {
				$paid_for	=	array_merge($paid_for,$saleAdvance);
			}  
///		}
		
		 
		///销售单平帐信息
	/*	$close_out		=	array('paid_for'=>$paid_for,'funds'=>$vo['funds'],'sale_id'=>$sale_id,'close_out'=>$post['close_out'],'close_out_comments'=>$post['close_out_comments']);	
		$saleCloseOut		=	$this->saleCloseOut($close_out); 
		if(is_array($saleCloseOut) && $saleCloseOut['id']>0) {
			$paid_for[]	=	$saleCloseOut['id'];
		} */
		
		if(count($paid_for)>1) { 
			///绑定记录  
 			$this->addPaidFor($this->comp_type,$paid_for);
		}
		unset($info);
		return $vo;
	}
	 
	/**
	 * 删除销售单
	 *
	 * @param int $id
	 * @return array
	 */
	public function deleteOp($id){   
		///删除销售单款项
		if($id>0) {
			$paid_id = $this
			->where('object_id='.$id.' and comp_type='.$this->comp_type.' and object_type='.$this->object_type.' and to_hide=1')
			->getField('id'); 
			///款项表ID
			if ($paid_id>0){
				$vo	=	$this->_deleteFunds($paid_id);  
			}
		} 
		///删除预付款款项
		Funds($id,121,'delete');
		///删除销售单平帐款项
///		Dzh::Funds($id,122,'delete');
		return $vo;
	}
	
	/**
	 * 销售单欠款信息
	 *
	 * @param int $id 销售单ID
	 * @return array
	 */
	public function fundInfo($id) {  
		if($id>0) {
			$model			=	M($this->getCompPaidDetail($this->comp_type));
			///销售单欠款
			$info['funds'] 	= _formatList($this
			->where('object_id='.$id.' and comp_type='.$this->comp_type.' and object_type='.$this->object_type.' and to_hide=1')
			->select());   
			///预付款信息
			$info['advance']			=	Funds($id,121,'fundInfo');///预付款信息
			$info['advance_have_paid']	=	Funds($id,121,'getAdvanceTotalMoney');//////预付款信息总金额
			///平帐信息
///			$info['close_out']	=	Dzh::Funds($id,122,'fundInfo');///平帐信息 
			///销售单款项信息
			$info['sale_order_paid']	=	_formatArray($model->field('sum(should_paid*income_type*-1) as need_paid')->where('object_type in (120,121,122) and object_id='.$id)->find());      
			return $info;
		}
	}
	
	/**
	 * 销售单欠款
	 *
	 * @param int $id
	 * @return array
	 */
	public function saleFunds($id){
		if($id>0) {
			$this->format_data = false; 
			$model	= M('saleOrderDetail');
		 	$info	= $model
		 			->field( 	'b.basic_id as basic_id,b.currency_id as currency_id,date(b.order_date) as paid_date,
								b.client_id as comp_id,'.$this->comp_type.' as comp_type,b.id as object_id,b.sale_order_no as reserve_id,
								(sum(quantity*capability*dozen*discount*price)-b.pr_money) as money,
								0 as account_money,
								c.paid_id as id,
								'.$this->object_type.' as object_type
								')
							->join('as a 
									left join sale_order as b on a.sale_order_id=b.id and b.id='.$id.' 
									left join '.$this->object_type_table.' as c 
										on c.object_id=a.sale_order_id and c.object_type='.$this->object_type.' ')
							->where('a.sale_order_id='.$id.' ')
							->group('a.sale_order_id')
							->find();  
///							echo $model->getLastSql(); 
///							exit;  
			 
				///如果开启发货模块 
				if (C('sale.relation_sale_follow_up')==1){///销售是否有后续流程 
					$info['state']	=	0;
				}else{
					$info['state']	=	1;
				}    
				$info['id']		=	 $this->_saveFunds($info);   
				return $info;  
		}
	}
	
	/**
	 * 销售单预付款
	 *
	 * @param array $info
	 * @return array
	 */
	public function saleAdvance($info) { 
		return Funds($info,121);
	} 
	 
	/**
	 * 销售单平帐信息
	 *
	 * @param array $info
	 */
	public function saleCloseOut($info) { 
///		return Funds($info,122);
	} 
	
	/**
	 * 插入前的业务规则验证
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkInsert($info){    
		return	$this->checkClientDate($info);  
	}
	 
	/**
	 * 修改插入数据前的业务规则验证
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkUpdate($info){ 
		return	$this->checkClientDate($info);  
	} 
	
	/**
	 * 验证某日期以前的款项是否可以被插入
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkClientDate($info){
		$error['state']	=	1;    
		if ($this->validCheckAccountDate(formatDate($info['order_date'],'vf_date'),$info)===false){
			$error['state']	=	-1;
			$error['error_msg'][]	=	$info['order_date'].L('order_date_chk');///已指定操作,不可删除
		}  
		return $error;
	}
	 
	/**
	 * 验证是否可修改
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkEdit($info) {  
		return $this->checkHaveCloseOut($info); 
	}
	 
	/**
	 * 验证是否可删除
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkDelete($info) {  
		return $this->checkHaveCloseOut($info);
	}
	 
	/**
	 * 验证多个厂家是否被对账或总平或者指定支付
	 *
	 * @param array $info
	 * @return array
	 */
	private function checkHaveCloseOut($info) { 
		$error['state']	=	1; 
		///销售单id
		$id	=	is_array($info)?$info['id']:$info; 
		///验证是否被对账或者总平
		if ($id>0) { 
			///销售单删除验证
			$error	=	$this->_checkSaleOrder($id); 
		}else {
			$error['state']			=	-1; 
			$error['error_msg'][]	=	L('error_funds_values_error');///款项验证,参数错误,请重新操作
		}   
		return $error; 
	}
	
	 
}
?>